<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="边坡编号" prop="slopeName">
        <el-select v-model="queryParams.slopeName" placeholder="请选择边坡编号" clearable style="width: 200px">
          <el-option
              v-for="dict in slopeCodeList"
              :key="dict.key"
              :label="dict.key"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="测点编号" prop="pointNumber">
        <el-select v-model="queryParams.pointNumber" placeholder="请选择边坡编号" clearable style="width: 200px">
          <el-option
              v-for="dict in monitorIds"
              :key="dict.key"
              :label="dict.key"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警等级" prop="warningLevel">
        <el-select v-model="queryParams.warningLevel" placeholder="请选择预警等级" clearable style="width: 200px">
          <el-option
              v-for="dict in warning_level"
              :key="dict.key"
              :label="dict.key"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警时间" prop="warningTime">
        <el-date-picker clearable
          v-model="queryParams.warningTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预警时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预警处置" prop="warningDisposal">
      <el-select v-model="queryParams.warningDisposal" placeholder="请选择预警处置情况" clearable style="width: 200px">
        <el-option
            v-for="dict in disposal_status"
            :key="dict.key"
            :label="dict.label"
            :value="dict.value"
        />
      </el-select>
    </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['warning:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['warning:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warning:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['warning:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="边坡名称" fixed="left" align="center" prop="slopeName" width="280px">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeName"/>
        </template>
      </el-table-column>
      <el-table-column label="监测点编号" fixed="left" align="center" prop="pointNumber" width="200px">
        <template #default="scope">
          <dict-tag :options="monitorIds" :value="scope.row.pointNumber"/>
        </template>
      </el-table-column>
      <el-table-column label="预警等级" fixed="left" align="center" prop="warningLevel" width="280px">
        <template #default="scope">
          <dict-tag :options="warning_level" :value="scope.row.warningLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="预警信息" align="center" prop="warningInfo">
        <template #default="scope">
          <div v-html="scope.row.warningInfo"></div>
        </template>
      </el-table-column>

      <el-table-column label="预警时间" align="center" prop="warningTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warningTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警处置" align="center" prop="warningDisposal" width="180">
        <template #default="scope">
          <dict-tag :options="disposal_status" :value="scope.row.warningDisposal"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['warning:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['warning:info:remove']">删除</el-button>
          <el-button link type="primary" plain icon="Plus" @click="handleAddDisposal(scope.row)" v-hasPermi="['warning:info:add']">确认接警</el-button>
          <el-button link type="primary" plain icon="Edit" @click="finish(scope.row)">报警处置</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="边坡编号" prop="slopeName">
          <el-select v-model="queryParams.slopeName" placeholder="请选择边坡编号" clearable style="width: 200px">
            <el-option
                v-for="dict in slopeCodeList"
                :key="dict.key"
                :label="dict.key"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="测点编号" prop="pointNumber">
          <el-select v-model="queryParams.pointNumber" placeholder="请选择边坡编号" clearable style="width: 200px">
            <el-option
                v-for="dict in monitorIds"
                :key="dict.key"
                :label="dict.key"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="预警等级" prop="warningLevel">
          <el-input v-model="form.warningLevel" placeholder="请输入预警等级" />
        </el-form-item>
        <el-form-item label="预警信息" prop="warningInfo">
          <el-input v-model="form.warningInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预警时间" prop="warningTime">
          <el-date-picker clearable
            v-model="form.warningTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择预警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预警处置" prop="warningDisposal">
          <el-input v-model="form.warningDisposal" placeholder="请输入预警处置" />
        </el-form-item>
        <el-form-item label="管养单位" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入管养单位" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加或修改处置记录对话框 -->
    <el-dialog :title="title" v-model="openDisposal" width="700px" append-to-body>
      <el-form ref="disposalRef" :model="disposalForm" label-width="100px">
        <el-row gutter="20">
          <el-col :span="12">
            <el-form-item label="边坡名称" prop="slopeName">
              <el-input v-model="disposalForm.slopeName" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测点编号" prop="pointerNumber">
              <el-input v-model="disposalForm.pointerNumber" readonly/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="预警时间" prop="warningTime">
              <el-input v-model="disposalForm.warningTime" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警等级" prop="warningLevel">
              <el-input v-model="disposalForm.warningLevel" readonly/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="预警信息" prop="warningInfo">
              <el-input v-model="disposalForm.warningInfo" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置单位" prop="deptId">
              <el-select v-model="disposalForm.deptId" placeholder="请选择处置单位">
                <el-option
                    v-for="dict in sys_dept_list"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="处置人员" prop="userId">
              <el-input v-model="disposalForm.userId" placeholder="请输入处置人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置开始时间" prop="disposalStartTime">
              <el-date-picker clearable
                              v-model="disposalForm.disposalStartTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择处置开始时间"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="到达现场时间" prop="arrivalTime">
              <el-date-picker clearable
                              v-model="disposalForm.arrivalTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择到达现场时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置措施" prop="disposalMeasures">
              <el-input
                  v-model="disposalForm.disposalMeasures"
                  type="textarea"
                  placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否疏散人员" prop="evacuated">
              <el-select v-model="disposalForm.evacuated" placeholder="请选择是否疏散人员">
                <el-option
                    v-for="dict in yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="疏散人数" prop="evacuatedNumber">
              <el-input v-model="disposalForm.evacuatedNumber" placeholder="请输入疏散人数" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="处置结果" prop="disposalResult">
              <el-input
                  v-model="disposalForm.disposalResult"
                  type="textarea"
                  placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结果评估" prop="evaluationLevel">
              <el-input v-model="disposalForm.evaluationLevel" placeholder="请输入结果评估" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否恢复通行" prop="resumed">
              <el-select v-model="disposalForm.resumed" placeholder="请选择是否恢复通行">
                <el-option
                    v-for="dict in yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="恢复时间" prop="resumeTime">
              <el-date-picker clearable
                              v-model="disposalForm.resumeTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择恢复时间"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                  v-model="disposalForm.remarks"
                  type="textarea"
                  placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDisposalForm()">确 定</el-button>
          <el-button @click="cancelDisposal">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Info">
import {listInfo, getInfo, delInfo, addInfo, updateInfo, finishAlarm} from "@/api/slope/warning/info";
import {addDisposal} from "@/api/slope/disposal.js";
import {listSlopeCode} from "@/api/slope/info/info.js";
import {listMonitorPoints} from "@/api/slope/monitorPoints/monitorpoints.js";

const { proxy } = getCurrentInstance();
const { sys_dept_list, yes_no, disposal_status, warning_level  } = proxy.useDict('sys_dept_list', 'yes_no','disposal_status', 'warning_level');

const monitorIds = ref([])
const slopeCodeList = ref([])
const infoList = ref([]);
const open = ref(false);
const openDisposal = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  disposalForm:{},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    slopeName: null,
    pointNumber: null,
    warningLevel: null,
    warningTime: null,
    warningDisposal: null,
  },
  rules: {
    slopeName: [
      { required: true, message: "边坡名称不能为空", trigger: "blur" }
    ],
    pointNumber: [
      { required: true, message: "测点编号不能为空", trigger: "blur" }
    ],
    warningLevel: [
      { required: true, message: "预警等级不能为空", trigger: "blur" }
    ],
    warningInfo: [
      { required: true, message: "预警信息不能为空", trigger: "blur" }
    ],
    warningTime: [
      { required: true, message: "预警时间不能为空", trigger: "blur" }
    ],
    warningDisposal: [
      { required: true, message: "预警处置不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "管养单位不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, disposalForm, rules } = toRefs(data);

/** 新增按钮操作 */
function handleAddDisposal(row) {
  openDisposal.value = true;
  title.value = "添加处置记录";
  disposalForm.value = {
    id:row.id,
    slopeName: row.slopeName,
    pointerNumber: row.pointNumber,
    warningTime: row.warningTime,
    warningLevel: row.warningLevel,
    warningInfo: row.warningInfo,
    deptId: null,
    userId: null,
    disposalStartTime: null,
    arrivalTime: null,
    disposalMeasures: null,
    evacuated: null,
    evacuatedNumber: null,
    disposalResult: null,
    evaluationLevel: null,
    resumed: null,
    resumeTime: null,
    remarks: null,
    createdBy: null,
    createdTime: null
  };
}

function submitDisposalForm() {
        addDisposal(disposalForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          openDisposal.value = false;
        });
  const _id = disposalForm.value.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
  });
  form.value.warningDisposal=1;
  form.value.id = disposalForm.value.id
  finishAlarm(form.value).then(response => {
    getList();
  });

}

function finish(row){
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
  });
  form.value.warningDisposal = 2;
  form.value.id = row.id;
  console.log(form.value)
  finishAlarm(form.value).then(response => {
    proxy.$modal.msgSuccess("处置完成");
    getList();
  });
}

// 取消按钮
function cancelDisposal() {
  openDisposal.value = false;
  resetDisposal();
}

// 表单重置
function resetDisposal() {
  disposalForm.value = {
    id: null,
    slopeName: null,
    pointerNumber: null,
    warningTime: null,
    warningLevel: null,
    warningInfo: null,
    deptId: null,
    userId: null,
    disposalStartTime: null,
    arrivalTime: null,
    disposalMeasures: null,
    evacuated: null,
    evacuatedNumber: null,
    disposalResult: null,
    evaluationLevel: null,
    resumed: null,
    resumeTime: null,
    remarks: null,
    createdBy: null,
    createdTime: null
  };
  proxy.resetForm("disposalRef");
}

/** 查询预警信息列表 */
function getList() {
  loading.value = true;
  listMonitorPoints().then(response => {
    monitorIds.value = Object.entries(response).map(([key, value]) => ({
      label: key,
      value: value
    }))
  })
  listSlopeCode().then(response => {
    slopeCodeList.value = Object.entries(response).map(([key, _]) => ({
      label: key,
      value: key
    }))
  })
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    slopeName: null,
    pointNumber: null,
    warningLevel: null,
    warningInfo: null,
    warningTime: null,
    warningDisposal: null,
    deptId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预警信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预警信息编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('warning/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
